<template>
  <div>
    <BasicTable @register="registerTable">
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                label: '修改',
                tooltip: '修改',
                onClick: handleAdd.bind(null, CustomModalTypeEnum.Edit, record),
              },
              {
                label: '新增子类目',
                tooltip: '新增子类目',
                ifShow: () => record.level !== 3,
                onClick: handleAdd.bind(null, CustomModalTypeEnum.AddChild, {
                  parentId: record.id,
                }),
              },
              {
                label: '删除',
                tooltip: '删除',
                onClick: handleDel.bind(null, record),
              },
            ]"
        /></template>
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handleAdd(CustomModalTypeEnum.Add)">新建</a-button>
      </template>
      <template #name="{ record }">
        <p v-if="record.level === 1" style="text-align: left; padding-left: 100px">{{
          record.name
        }}</p>
        <p v-else-if="record.level === 2" style="text-align: left; padding-left: 120px">{{
          record.name
        }}</p>
        <p v-else style="text-align: left; padding-left: 140px">{{ record.name }}</p>
      </template>
      <template #status="{ record }">
        <Badge
          v-if="record.status === DataStatusEnum.Normal"
          status="success"
          :text="dataStatusEnumText[DataStatusEnum.Normal]"
        />
        <Badge v-else status="default" :text="dataStatusEnumText[DataStatusEnum.Disable]" />
      </template>
      <template #level="{ record }">
        <Tag v-if="record.level === 1" color="processing"> 一级 </Tag>
        <Tag v-else-if="record.level === 2" color="success"> 二级 </Tag>
        <Tag v-else color="gold"> 三级 </Tag>
      </template>
    </BasicTable>
    <AddedModal @register="register" @reload="reload" />
  </div>
</template>

<script setup lang="ts">
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { getBasicColumns } from './tableData';
  import { Badge, Modal, Tag } from 'ant-design-vue';
  import { CustomModalTypeEnum, DataStatusEnum, dataStatusEnumText } from '@/enums/publicEnum';
  import { useMessage } from '@/hooks/web/useMessage';
  import { useModal } from '@/components/Modal';
  import AddedModal from './modal/added.vue';
  import {
    productCategoryDel,
    productCategoryList,
    productCategorySwap,
  } from '@/api/hby/categoryManagement';
  import { CategorytListResult } from '@/api/hby/categoryManagement/types';

  const { createMessage } = useMessage();

  const [registerTable, { reload }] = useTable({
    title: '',
    api: productCategoryList,
    columns: getBasicColumns(),
    rowKey: 'id',
    formConfig: {
      labelWidth: 100,
      schemas: [
        {
          field: 'name',
          label: '关键词',
          component: 'Input',
          componentProps: {
            placeholder: '请输入类目名称或者类目关键词',
          },
          colProps: {
            span: 5,
          },
        },
        // {
        //   field: 'status',
        //   label: '状态',
        //   component: 'Select',
        //   colProps: {
        //     span: 5,
        //   },
        //   componentProps: {
        //     options: [
        //       { label: dataStatusEnumText[DataStatusEnum.Normal], value: DataStatusEnum.Normal },
        //       { label: dataStatusEnumText[DataStatusEnum.Disable], value: DataStatusEnum.Disable },
        //     ],
        //   },
        // },
      ],
    },
    canRowDrag: true,
    beforeRowDragChange: async (source, target) => {
      try {
        await productCategorySwap([source.id, target.id]);
        reload();
        return false;
      } catch (error) {
        return false;
      }
    },
    useSearchForm: true,
    showTableSetting: true,
    tableSetting: { fullScreen: true },
    showIndexColumn: false,
    clickToRowSelect: false,
    pagination: {
      pageSize: 20,
    },
    actionColumn: {
      width: 120,
      title: '操作',
      fixed: 'right',
      dataIndex: 'action',
    },
    scroll: { x: 1300 },
  });

  const [register, { openModal }] = useModal();

  function handleAdd(type: CustomModalTypeEnum, row?: Partial<CategorytListResult>) {
    openModal(true, {
      type,
      row,
    });
  }
  function handleDel(row: CategorytListResult) {
    Modal.confirm({
      title: '提示',
      content: '该操作将删除该类目',
      onOk: async () => {
        row.id;
        await productCategoryDel(row.id);
        createMessage.success('删除成功！');
        reload();
      },
    });
  }
</script>
